from spargameproject.spargame.game.engine.game_engine import PlayHandler
from spargameproject.spargame.game.playhandlers.spar_play_handlers import SINGLE_PLAYER, getSparDeck
from spargameproject.spargame.models import Player, GamePlayer

class SparInitHandler(PlayHandler):
  def getType(self):
    return 'init_game'


  def handlePlay(self, play, game_engine):
    print 'Just handled a play of type: ' + self.getType()
    play.setMetadata('message', 'Started a new game')
    session = play.game.getMetadata('session')
    play.setMetadata('session', session)
    response = {}
    game = play.game
    if game.getMetadata('game_type') == SINGLE_PLAYER:
      bot_player = Player.objects.create(user=game_engine.getBot(), name='Bot')
      bot_player.setMetadata('status', 'connected')
      bot_player.setMetadata('avatar_img', '/site_media/images/avatars/shuffled.jpg')
      bot_game_player = GamePlayer.objects.create(game=play.game, player=bot_player)
      bot_game_player.setMetadata('ready', True)
      print "player %s is: %s" % (bot_game_player.player.id, bot_game_player.getMetadata("ready"))
      bot_game_player.setMetadata('is_bot', True)
      bot_game_player.setMetadata('bot_type', game.getMetadata('game_level'))
      bot_game_player.setMetadata('status', 'connected')

    game_deck = getSparDeck()
    game.setMetadata('game_deck', game_deck)
    game.setMetadata('session', 0)
    game.setMetadata('dealer', game.creator_player.id)
    game.setMetadata('turn_player', game.creator_player.id)
    return [], response
